<template>
  <div class="container">
    <h1>人员列表</h1>
    <h3>列表中一个人的名字是{{ firstPersonName }}</h3>
    <h1 style="color: red">Count组件求和为{{ sum }}</h1>
    <input type="text" placeholder="请输入人员姓名" v-model="name" />
    <button @click="addPerson">点击确认</button>
    <button @click="addWang">添加一个姓王的人</button>
    <button @click="addPersonServer">添加一个随机人</button>
    <ul>
      <li v-for="p in person" :key="p.id">{{ p.name }}</li>
    </ul>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
export default {
  name: "Person",
  data() {
    return {
      name: "",
    };
  },
  computed: {
    person() {
      return this.$store.state.PersonAbout.persons;
    },
    sum() {
      return this.$store.state.CountAbout.sum;
    },
    firstPersonName() {
      return this.$store.getters["PersonAbout/firstPersonName"];
    },
  },
  components: {},
  methods: {
    addPerson() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.commit("PersonAbout/addPerson", personObj);
      this.name = "";
    },
    addWang() {
      const personObj = { id: nanoid(), name: this.name };
      this.$store.dispatch("PersonAbout/addPersonWang", personObj);
      this.name = "";
    },
    addPersonServer() {
      this.$store.dispatch("PersonAbout/addPersonServer");
    },
  },
};
</script>

